package whf.framework.meta.service;

import java.util.List;

import whf.framework.exception.CreateException;
import whf.framework.exception.FindException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.exception.UpdateException;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.MetaNotFoundException;
import whf.framework.meta.dao.ObjectDAO;
import whf.framework.meta.dao.ObjectPropertyDAO;
import whf.framework.meta.entity.Object;
import whf.framework.meta.entity.ObjectProperty;
import whf.framework.util.BeanFactory;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-05-31
 */
public class ObjectServiceImp extends whf.framework.service.ServiceImp<Object> implements ObjectService {

	private static Log log = LogFactory.getLog(ObjectServiceImp.class);
	
	private ObjectDAO objectDAO;
	
	private ObjectPropertyDAO objectPropertyDAO;

	public void setObjectDAO(ObjectDAO objectDAO) {
		this.objectDAO = objectDAO;
	}

	public void setObjectPropertyDAO(ObjectPropertyDAO objectProjectDAO) {
		this.objectPropertyDAO = objectProjectDAO;
	}

	public static ObjectService getObjectService() throws ServiceNotFoundException {
		return (ObjectService)BeanFactory.getService(ObjectServiceImp.class);
	}

	/* (non-Javadoc)
	 * @see whf.framework.service.ServiceImp#getBoClass()
	 */
	@Override
	public Class<Object> getEntityClass() {
		return Object.class;
	}

	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ObjectService#findByBoClass(java.lang.String)
	 */
	public Object findByEntityClass(String cls) throws FindException {
		return this.objectDAO.findByEntityClass(cls);
	}
	
	/**
	 * @modify wanghaifeng Oct 29, 2006 8:37:55 AM
	 * @param propertyName
	 * @param className
	 * @return
	 * @throws FindException
	 * @throws MetaNotFoundException
	 */
	public Object findByClassName(String propertyName, String className) throws FindException, MetaNotFoundException{
		List list = this.objectDAO.findWithoutLazy("t."+propertyName+"=?", className);
		if(list.size() > 0){
			return (Object)list.get(0);
		} else {
			return null;
		}
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ObjectService#findByActionClass(java.lang.String)
	 */
	public Object findByActionClass(String actionClass) throws FindException{
		return this.findByClassName("actionClassName", actionClass);
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ObjectService#findByServiceClass(java.lang.String)
	 */
	public Object findByServiceClass(String cls) throws FindException{
		return this.findByClassName("serviceClassName", cls);
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ObjectService#findByDaoClass(java.lang.String)
	 */
	public Object findByDaoClass(String cls) throws FindException{
		return this.findByClassName("daoClassName", cls);
	}

	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ObjectService#addProperty(whf.framework.meta.entity.Object, whf.framework.meta.entity.ObjectProperty)
	 */
	public void addProperty(Object object, ObjectProperty property) throws CreateException, UpdateException {
		if(property.getId() == 0){
			this.objectPropertyDAO.create(property);
		}
		object.addProperty(property);
		this.objectDAO.update(object);
	}

	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ObjectService#findByTableName(java.lang.String)
	 */
	public Object findByTableName(String tableName) throws FindException {
		return objectDAO.findByTableName(tableName);
	}
} 